package com.example.aboutcats.companionobj

class Director(
                val firstName: String,
                val lastName: String,
                val yearOfBirth: Int
              ) {

  def name = s"$firstName $lastName"

  def copy(
            firstName: String = this.firstName,
            lastName: String = this.lastName,
            yearOfBirth: Int = this.yearOfBirth
          ) = new Director(firstName, lastName, yearOfBirth)

}

object Director {

  def apply(firstName: String, lastName: String, yearOfBirth: Int) =
    new Director(firstName, lastName, yearOfBirth)

  def older(director1: Director, director2: Director): Director =
    if (director1.yearOfBirth < director2.yearOfBirth) director1
    else director2


}

class Film(
            val name: String,
            val yearOfRelease: Int,
            val imdbRating: Double,
            val director: Director
          ) {

  def directorsAge = director.yearOfBirth - yearOfRelease

  def isDirectedBy(director: Director) = this.director == director

  def copy(name: String = this.name,
           yearOfRelease: Int = this.yearOfRelease,
           imdbRating: Double = this.imdbRating,
           director: Director = this.director) =
    new Film(name, yearOfRelease, imdbRating, director)

}

object Film {

  def apply(
             name: String,
             yearOfRelease: Int,
             imdbRating: Double,
             director: Director
           ): Film = new Film(name, yearOfRelease, imdbRating, director)

  def newer(film1: Film, film2: Film) =
    if (film1.yearOfRelease < film2.yearOfRelease) film2 else film1

  def highestRating(film1: Film, film2: Film): Double = {
    val rating1 = film1.imdbRating
    val rating2 = film2.imdbRating
    if (rating1 > rating2) rating1 else rating2
  }

  def oldestDirectorAtTheTime(film1: Film,film2: Film):Director =
    if(film1.directorsAge>film2.directorsAge) film1.director else film2.director

}
